//
//  LXSegmentTitleView.m
//

#import "LXSegmentTitleView.h"

@interface LXSegmentTitleView()

@property (nonatomic, weak) UIScrollView *scrollView;

@property (nonatomic, strong) NSMutableArray<UIButton *> *itemButtons;

@end

@implementation LXSegmentTitleView

- (void)awakeFromNib{
    [super awakeFromNib];
    [self initData];
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self initData];
    }
    return self;
}

- (void)initData{
    self.titleNormalColor = [UIColor blackColor];
    self.titleSelectedColor = [UIColor redColor];
    self.selectedIndex = 0;
    self.titleFont = [UIFont systemFontOfSize:15.f];
    self.itemMinMargin = 25.f;
    self.alpha = 0.6;
    self.titleSelectFont = self.titleFont;
}

- (void)layoutSubviews{
    
    [super layoutSubviews];
    self.scrollView.frame = self.bounds;
    if (self.itemButtons.count == 0) {
        return;
    }
    

    UIFont *titleFont = _titleFont;
    if (_titleFont != _titleSelectFont) {
        for (int idx = 0; idx < self.itemButtons.count; idx++) {
            UIButton *btn = self.itemButtons[idx];
            titleFont = btn.isSelected?_titleSelectFont:_titleFont;
            btn.alpha = btn.isSelected ? 1:self.alpha;
        }
    }
    
    CGFloat fixL = 15;
    CGFloat fixM = 5;
    CGFloat itemBtnWidth = (kScreenWidth - fixL - fixL - (self.itemButtons.count- 1)*fixM) /self.itemButtons.count;
    
    for (int i=0; i<self.itemButtons.count; i++) {
        
        UIButton *btn = self.itemButtons[i];
        [btn sizeToFit];
    
        btn.frame = CGRectMake(i*itemBtnWidth + fixL + fixM*i, 0,itemBtnWidth, self.scrollView.frame.size.height - 15);
       
    }
    
    self.scrollView.contentSize = CGSizeMake(CGRectGetWidth(self.bounds), self.scrollView.frame.size.height);
    
}


- (void)scrollRectToVisibleCenterAnimated:(BOOL)animated {
    UIButton *selectedBtn = self.itemButtons[self.selectedIndex];
    CGRect centeredRect = CGRectMake(selectedBtn.center.x - self.scrollView.frame.size.width / 2, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height);
    [self.scrollView scrollRectToVisible:centeredRect animated:animated];
    
    UIFont *titleFont = _titleFont;
    UIButton *selectBtn = self.itemButtons[self.selectedIndex];
    titleFont = selectBtn.isSelected?_titleSelectFont:_titleFont;
    
}

#pragma mark - getter

- (UIScrollView *)scrollView{
    if (!_scrollView) {
        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
        scrollView.scrollsToTop = NO;
        scrollView.showsHorizontalScrollIndicator = NO;
        scrollView.showsVerticalScrollIndicator = NO;
        [self addSubview:scrollView];
        _scrollView = scrollView;
    }
    return _scrollView;
}


- (NSMutableArray<UIButton *> *)itemButtons{
    if (!_itemButtons) {
        _itemButtons = [[NSMutableArray alloc] init];
    }
    return _itemButtons;
}


#pragma mark - setter

- (void)setSegmentTitles:(NSArray<NSString *> *)segmentTitles{
    [self.itemButtons makeObjectsPerformSelector:@selector(removeFromSuperview)];
    self.itemButtons = nil;
    for (NSString *title in segmentTitles) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = 888 + self.itemButtons.count;
        [btn setTitle:title forState:UIControlStateNormal];
        [btn setTitleColor:self.titleNormalColor forState:UIControlStateNormal];
        [btn setTitleColor:self.titleSelectedColor forState:UIControlStateSelected];
        
        [btn setBackgroundImage:self.btnBackNormalImg forState:UIControlStateNormal];
        [btn setBackgroundImage:self.btnBackSelectImg forState:UIControlStateSelected];
        
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.titleLabel.font = self.titleFont;
        btn.alpha = self.alpha;
        [self.scrollView addSubview:btn];
        if (self.selectedIndex == self.itemButtons.count) {
            btn.selected = YES;
            btn.alpha = 1;
            btn.titleLabel.font = self.titleSelectFont;
        }
        [self.itemButtons addObject:btn];
        
    }
    [self setNeedsLayout];
    [self layoutIfNeeded];
}


- (void)setSelectedIndex:(NSInteger)selectedIndex{
    if (_selectedIndex == selectedIndex || selectedIndex < 0 || selectedIndex > self.itemButtons.count - 1 || self.itemButtons.count <= 0) {
        return;
    }
    UIButton *btn = [self.scrollView viewWithTag:_selectedIndex + 888];
    btn.selected = NO;
    btn.alpha = self.alpha;
    btn.titleLabel.font = _titleFont;
    _selectedIndex = selectedIndex;
    UIButton *selectedBtn = [self.scrollView viewWithTag:_selectedIndex + 888];
    selectedBtn.selected = YES;
    selectedBtn.titleLabel.font = _titleSelectFont;
    selectedBtn.alpha  = 1;
   
    
    
}

- (void)setTitleNormalColor:(UIColor *)titleNormalColor{
    _titleNormalColor = titleNormalColor;
    for (UIButton *btn in self.itemButtons) {
        [btn setTitleColor:titleNormalColor forState:UIControlStateNormal];
    }
}

- (void)setTitleSelectedColor:(UIColor *)titleSelectedColor{
    _titleSelectedColor = titleSelectedColor;
    for (UIButton *btn in self.itemButtons) {
        [btn setTitleColor:titleSelectedColor forState:UIControlStateSelected];
    }
}
    
- (void)setBtnBackNormalImg:(UIImage *)btnBackNormalImg{
    _btnBackNormalImg = btnBackNormalImg;
    for (UIButton *btn in self.itemButtons) {
        [btn setBackgroundImage:btnBackNormalImg forState:UIControlStateNormal];
    }
}
- (void)setBtnBackSelectImg:(UIImage *)btnBackSelectImg{
    _btnBackSelectImg = btnBackSelectImg;
    for (UIButton *btn in self.itemButtons) {
        [btn setBackgroundImage:btnBackSelectImg forState:UIControlStateSelected];
    }
}

- (void)setTitleFont:(UIFont *)titleFont{
    _titleFont = titleFont;
    for (UIButton *btn in self.itemButtons) {
        btn.titleLabel.font = titleFont;
    }
    [self setNeedsLayout];
    [self layoutIfNeeded];
}

- (void)setTitleSelectFont:(UIFont *)titleSelectFont{
    if (_titleFont == titleSelectFont) {
        _titleSelectFont = _titleFont;
        return;
    }
    _titleSelectFont = titleSelectFont;
    for (UIButton *btn in self.itemButtons) {
        btn.titleLabel.font = btn.isSelected?titleSelectFont:_titleFont;
    }
    
    [self setNeedsLayout];
    [self layoutIfNeeded];
}




#pragma mark - EventResponse
- (void)btnClick:(UIButton *)btn{
    NSInteger btnIndex = btn.tag - 888;

    if (btnIndex == self.selectedIndex) {
        return;
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(segmentTitleView:selectedIndex:lastSelectedIndex:)]) {
        [self.delegate segmentTitleView:self selectedIndex:btnIndex lastSelectedIndex:self.selectedIndex];
    }
    self.selectedIndex = btnIndex;
  
}
- (void)setAlpha:(CGFloat)alpha{
    _alpha = alpha;
}


#pragma mark Private
+ (CGFloat)getWidthWithString:(NSString *)string font:(UIFont *)font {
    NSDictionary *attrs = @{NSFontAttributeName : font};
    return [string boundingRectWithSize:CGSizeMake(0, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size.width;
}

@end
